
let Compiler = require("./Compiler");
function webpack(options){
    // 1. 初始化参数：从配置文件和 Shell 语句中读取并合并参数,得出最终的配置对象（shell传参就是通过全局命令webpack --XXXX来进行传参）(shell传参优先级更高)
    let argv = process.argv.slice(2); //取出索引1之后的值
    let shellOptions =  argv.reduce((shellOptions,option)=>{
        let [key, value] = option.split("=");
        shellOptions[key.slice(2)] = value
        return shellOptions 
    },{})
    // 源码中使用webpack-merge专门用来合并配置文件
    let finalOptions = { ...options, ...shellOptions }; //将shell和webpack.config.js合并
    console.log("finalOptions", finalOptions);
    //2.用上一步得到的参数初始化Compiler对象
    let compiler = new Compiler(finalOptions);
    // 3. 加载所有配置的插件
    let { plugins } = finalOptions;
    for (let plugin of plugins) {
        plugin.apply(compiler);
    }
    return compiler
}
module.exports = webpack